package com.lindroid.androiduilearning.expandablelistview;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.lindroid.androiduilearning.R;
import com.lindroid.androiduilearning.base.BaseFragment;
import com.lindroid.androiduilearning.expandablelistview.adapter.MajorAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author Lin
 */
public class MajorFragment extends BaseFragment {

    private List<String> groupList = new ArrayList<>();
    private List<List<String>> childList = new ArrayList<>();
    private ExpandableListView expandableList;

    public MajorFragment() {
        // Required empty public constructor
    }

    public static MajorFragment newInstance() {
        Bundle args = new Bundle();
        MajorFragment fragment = new MajorFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_major, container, false);
        initView(rootView);
        initAdapter();
        return rootView;
    }

    private void initData() {
        //初始化一级专业数据
        for (int i = 1; i <= 15; i++) {
            groupList.add(new StringBuffer("一级专业").append(i).toString());
        }
        //初始化二级专业数据
        Random random = new Random();
        for (String s : groupList) {
            List<String> childDatas = new ArrayList<>();
            int size = random.nextInt(10) + 5;
            for (int i = 1; i <= size; i++) {
                childDatas.add(new StringBuffer("二级专业").append(i).toString());
            }
            childList.add(childDatas);
        }
        
    }

    private void initAdapter() {
        MajorAdapter adapter = new MajorAdapter(context, groupList, childList);
        expandableList.setAdapter(adapter);
    }

    private void initView(View rootView) {
        expandableList = rootView.findViewById(R.id.expandable_list);
        //子选项的点击事件
        expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                                        int childPosition, long id) {
                String toastStr = new StringBuffer("你选择了").append(groupList.get(groupPosition))
                        .append("的").append(childList.get(groupPosition).get(childPosition)).toString();
                Toast.makeText(context, toastStr, Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        //组选项的点击事件
        expandableList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                String toastStr = new StringBuffer("你选择了").append(groupList.get(groupPosition)).toString();
                Toast.makeText(context, toastStr, Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        //实现每次只展开一个组选项列表的功能
        expandableList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                //获取组选项个数
                int groupSize = expandableList.getExpandableListAdapter().getGroupCount();
                for (int i = 0; i < groupSize; i++) {
                    if (i != groupPosition && expandableList.isGroupExpanded(i)) {
                        //不是当前点击的组选项且处于展开状态的就关闭
                        expandableList.collapseGroup(i);
                    }
                }
            }
        });

    }
}
